package wj.d908;

public class P2 {
    public int max(int[] nums, int n) {
        int[] result = new int[1];
        bt(nums, 0, Math.min(30, n), 0, result);
        return result[0];
    }

    private void bt(int[] nums, int begin, int canUsed, int now, int[] result) {
        if (begin == nums.length || canUsed == 0) {//结束
            return;
        } else {
            int num = nums[begin];
            if ((num | now) > result[0]) {
                result[0] = num | now;
            }
            //选
            bt(nums, begin + 1, canUsed - 1, now | num, result);
            //不选
            bt(nums, begin + 1, canUsed, now, result);
        }
    }

    public static void main(String[] args) {
        System.out.println(new P2().max(new int[]{5, 9, 1}, 3));
    }
}
